*******************************************************

/*  The Impact of the US-China Trade War on Vietnam's Labor Market
 Pham Phuong Ngoc and Dainn Wie
 
 This is do file create all of the tables and figures in the Article version.

Inputs:

1. master.dta 

*/

/*Packges to Install

ssc install winsor2
ssc install reghdfe
ssc install ftools
ssc install outreg2
ssc install egenmore

*/

clear all
capture log close
set more off

cd "C:\Replication"

********************************************************************************
*** Table 1: The Share of Informal and Uninsured Workers in Vietnam

use "Data\master.dta", clear

*keep 2017, 2018 and 2019
drop if year<=2016

* Share of informal workers overtime
preserve
* interact these indicator variables with the household sampling weight
generate hhwt_noag=weight_final * noag
generate hhwt_trading=weight_final * trading

* interact the household sampling weights with the indicator for informal workers
generate hhwt_informal=weight_final * informal
generate hhwt_trading_informal=hhwt_trading * informal
generate hhwt_noag_informal=hhwt_noag * informal

* create a national level 
collapse (sum) weight_final hhwt_*, by (year)

* calculate the share of informal workers
gen sinformal = (hhwt_informal/weight_final) 
label variable sinformal "All"

gen sinformal_noag = (hhwt_noag_informal/hhwt_noag)
label variable sinformal_noag "Non-Agriculture"

gen sinformal_trading = (hhwt_trading_informal/hhwt_trading)
label variable sinformal_trading "Trading"

* keep the share and year variables
keep sinformal sinformal_noag sinformal_trading year

* export to excel file
export excel using "Tables\Table1A.xlsx", first(varl) replace
restore

* Share of uninsured workers overtime
preserve
* interact these indicator variables with the household sampling weight
generate hhwt_noag=weight_final * noag
generate hhwt_trading=weight_final * trading

* interact the household sampling weights with the indicator for uninsured workers
generate hhwt_uninsured=weight_final * uninsured
generate hhwt_trading_uninsured=hhwt_trading * uninsured
generate hhwt_noag_uninsured=hhwt_noag * uninsured

* create a national level 
collapse (sum) weight_final hhwt_*, by (year)

* calculate the share of unsurance workers
gen suninsured = (hhwt_uninsured/weight_final) 
label variable suninsured "All"

gen suninsured_noag = (hhwt_noag_uninsured/hhwt_noag)
label variable suninsured_noag "Non-Agriculture"

gen suninsured_trading = (hhwt_trading_uninsured/hhwt_trading)
label variable suninsured_trading "Trading"

* keep the share and year variables
keep suninsured suninsured_noag suninsured_trading year

* export to excel file
export excel using "Tables\Table1B.xlsx", first(varl) replace
restore

********************************************************************************
*** Table 2: Decomposing changes in employment between 2017 and 2019
* Panel A: Decomposition changes in informal employment

preserve
* interact the informal dummies using sample weight
replace informal=informal*weight_final

* create an industry-year dataset
collapse (sum) weight_final informal, by (year isic3)

* calculate the share of employees working as informal workers within each industry
replace informal = informal / weight_final
label variable informal "Share of informal workers"

* calculate employment shares for each industry
egen total = sum(weight_final), by (year)
generate share = weight_final/total
label variable share "Share of total employment"
drop total weight_final

* save dataset
sort year isic3
save "Data\industry_informal_allind.dta", replace

* create a dataset without agriculture and fishing 
* drop agriculture and fishing
keep if isic3>32

* rescale the employment shares
egen total = total(share), by (year)
replace share=share / total
drop total
* save dataset
sort year isic3
save "Data\industry_informal_noag.dta", replace
restore

* create a dataset of only trading
preserve
* interact the informal dummies using sample weight
replace informal=informal*weight_final

* create an industry-year dataset
collapse (sum) weight_final informal, by (year isic3 trading)

* calculate the share of employees working as informal workers
replace informal = informal / weight_final
label variable informal "Share of informal workers"

* calculate employment shares for each industry
egen total = sum(weight_final), by (year)
generate share = weight_final/total
label variable share "Share of total employment"
drop total weight_final

* keep only trading industries
keep if trading==1
* rescale the employment shares
egen total = total(share), by (year)
replace share=share / total
drop total
* save dataset
sort year isic3
save "Data\industry_informal_trading.dta", replace

* loop through (1) all industries, (2) no agriculture and fishing, and (3) trading
drop if year==2018

foreach ind in allind noag trading {
	* Open the industry dataset
	use "Data\industry_informal_`ind'.dta", clear
	* reshare the data into wide format
	reshape wide informal* share*, i(isic3) j(year)
	* calculate average employment shares: Ej
	generate Ej=0.5*(share2017 + share 2019)
	* calculate average informal within an industry: ij
	generate ij = 0.5*(informal2017 + informal2019)
	* calculate the change in employment share
	generate dEj = share2019 - share2017
	* calculate the change in informal within an industry
	generate dij = informal2019 - informal2017
	* multiply the terms toghether and then sum over all industries
	generate within= dij*Ej
	generate between=dEj*ij 
	collapse (sum) within* between*
	generate period="2017-2019"
	* calculate the total changes
	generate total1=within + between
	* reorder the dataset
	order period within between total1
	* save the dataset
	save "Data\2017 2019 change in informal - `ind'.dta", replace	
}
* append them together
use "Data\2017 2019 change in informal - allind.dta", clear
generate sample =1 
order sample
append using "Data\2017 2019 change in informal - noag.dta"
replace sample=2 if sample==.
append using "Data\2017 2019 change in informal - trading.dta"
replace sample=3 if sample==.
label define sample_lbl 1 "All industries" 2 "Excluding agriculture and fishing" 3 "Trading"
label values sample sample_lbl
label variable within "Within"
label variable between "Between"
label variable total1 "Total"
export excel using "Tables\Table2A.xlsx", first(varl) replace
* erase the temporary datasets
foreach ind in allind noag trading {
	erase "Data\2017 2019 change in informal - `ind'.dta"
	erase "Data\industry_informal_`ind'.dta"
}
restore


* Panel B: Decomposition changes in uninsured employment

preserve
* interact the uninsured dummies using sample weight
replace uninsured=uninsured*weight_final

* create an industry-year dataset
collapse (sum) weight_final uninsured, by (year isic3)

* calculate the share of employees working as unisured workers within each industry
replace uninsured = uninsured / weight_final
label variable uninsured "Share of uninsured workers"

* calculate employment shares for each industry
egen total = sum(weight_final), by (year)
generate share = weight_final/total
label variable share "Share of total employment"
drop total weight_final

* save dataset
sort year isic3
save "Data\industry_uninsured_allind.dta", replace

* create a dataset without agriculture and fishing 
* drop agriculture and fishing
keep if isic3>32

* rescale the employment shares
egen total = total(share), by (year)
replace share=share / total
drop total
* save dataset
sort year isic3
save "Data\industry_uninsured_noag.dta", replace
restore

* create a dataset of only trading
preserve
* interact the uninsured dummies using sample weight
replace uninsured=uninsured*weight_final

* create an industry-year dataset
collapse (sum) weight_final uninsured, by (year isic3 trading)

* calculate the share of employees working as uninsured workers within each industry
replace uninsured = uninsured / weight_final
label variable uninsured "Share of uninsured workers"

* calculate employment shares for each industry
egen total = sum(weight_final), by (year)
generate share = weight_final/total
label variable share "Share of total employment"
drop total weight_final

* keep only trading industries
keep if trading==1
* rescale the employment shares
egen total = total(share), by (year)
replace share=share / total
drop total
* save dataset
sort year isic3
save "Data\industry_uninsured_trading.dta", replace

* loop through (1) all industries, (2) no agriculture and fishing, and (3) trading
drop if year==2018

foreach ind in allind noag trading {
	* Open the industry dataset
	use "Data\industry_uninsured_`ind'.dta", clear
	* reshare the data into wide format
	reshape wide uninsured* share*, i(isic3) j(year)
	* calculate average employment shares: Ej
	generate Ej=0.5*(share2017 + share 2019)
	* calculate average uninsured within an industry: ij
	generate ij = 0.5*(uninsured2017 + uninsured2019)
	* calculate the change in employment share
	generate dEj = share2019 - share2017
	* calculate the change in uninsured within an industry
	generate dij = uninsured2019 - uninsured2017
	* multiply the terms toghether and then sum over all industries
	generate within= dij*Ej
	generate between=dEj*ij 
	collapse (sum) within* between*
	generate period="2017-2019"
	* calculate the total changes
	generate total1=within + between
	* reorder the dataset
	order period within between total1
	* save the dataset
	save "Data\2017 2019 change in uninsured - `ind'.dta", replace	
}
* append them together
use "Data\2017 2019 change in uninsured - allind.dta", clear
generate sample =1 
order sample
append using "Data\2017 2019 change in uninsured - noag.dta"
replace sample=2 if sample==.
append using "Data\2017 2019 change in uninsured - trading.dta"
replace sample=3 if sample==.
label define sample_lbl 1 "All industries" 2 "Excluding agriculture and fishing" 3 "Trading"
label values sample sample_lbl
label variable within "Within"
label variable between "Between"
label variable total1 "Total"
export excel using "Tables\Table2B.xlsx", first(varl) replace
* erase the temporary datasets
foreach ind in allind noag trading {
	erase "Data\2017 2019 change in uninsured - `ind'.dta"
	erase "Data\industry_uninsured_`ind'.dta"
}
restore

********************************************************************************
*** Table 3: Impact of Trade War on Workers in Vietnam

* Open LFS dataset
use "Data\master.dta", clear

* drop year 2016 and 2015
drop if year<=2016

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & isic3<=332
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

label variable wt_tariff "The weighted tariff difference"
label variable tariff_manu "Tariff difference * Manufacturing"
label variable manu "Manufacturing"
label variable exp "Experience"
label variable expsquare "Square of Experience"
label variable sex "Female"
label variable schoolyear "Schooling years"
label variable area "Urban"
label variable informal "Informal"
label variable uninsured "Uninsured"
label variable tariff_informal "Tariff difference * Informal workers"
label variable tariff_uninsured "Tariff difference * Uninsured workers"

* Run regression with dependent variable informal
reghdfe informal wt_tariff exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)

outreg2 using "Tables\Table3.doc", label dec(3) replace ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe informal wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table3.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe informal wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table3.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 


reghdfe uninsured wt_tariff exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table3.doc", label dec(3) append ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes)  

reghdfe uninsured wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table3.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe uninsured wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table3.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

********************************************************************************
*** Table 4: Impacts of Export on Workers in Vietnam Using Trade War as an Instrument

use "Data\trade_tariff.dta", clear

collapse (sum) m_val m_q1, by(year hs6)

rename m_val ex_val
rename m_q1 ex_qty

label var ex_val "annual export value from VN to US"
label var ex_qty "annual export volume from VN to US"

merge m:1 hs using "HS2012.dta"

tab _merge
keep if _merge==3
drop _merge

collapse (sum) ex_val ex_qty, by(year isic3)


label var ex_val "annual export value from VN to US"
label var ex_qty "annual export volume from VN to US"

tab year


save "Data\export.dta", replace


*When there is no matching isic3 code, we code export value and volume to be zero. 
*It should be done when it is matched to the main data. 

use "Data\master.dta", clear

merge m:1 year isic3 using "Data\export.dta"
drop if _merge==2
replace ex_val=0 if _merge==1
replace ex_qty=0 if _merge==1

gen ln_exval=log(ex_val+1)
gen ln_exqty=log(ex_qty+1)

label var ln_exval "log of export value (in million USD)"
label var ln_exqty "log of export volume (in million units)"
drop _merge

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & isic3<=332
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

label variable wt_tariff "The weighted tariff difference"
label variable tariff_manu "Tariff difference * Manufacturing"
label variable manu "Manufacturing"
label variable exp "Experience"
label variable expsquare "Square of Experience"
label variable sex "Female"
label variable schoolyear "Schooling years"
label variable area "Urban"
label variable informal "Informal"
label variable uninsured "Uninsured"
label variable tariff_informal "Tariff difference * Informal workers"
label variable tariff_uninsured "Tariff difference * Uninsured workers"


*Sample period: 2016, 2017, 2018, 2019
*IV: log of export value
drop if year<=2015

* Run regression with dependent variable informal
ivreghdfe informal (ln_exval=wt_tariff) exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table4.doc", label dec(3) replace ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 


ivreghdfe informal (ln_exval=wt_tariff) exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table4.doc", label dec(3) append ctitle(Excluding Agriculture) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 


ivreghdfe informal (ln_exval=wt_tariff) exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table4.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 


* Run regression with dependent variable uninsured

ivreghdfe uninsured (ln_exval=wt_tariff) exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table4.doc", label dec(3) append ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 


ivreghdfe uninsured (ln_exval=wt_tariff) exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table4.doc", label dec(3) append ctitle(Excluding Agriculture) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

ivreghdfe uninsured (ln_exval=wt_tariff) exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table4.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 


********************************************************************************
*** Table 5: Placebo Test Using Pre-Trade War Periods

* Open LFS dataset
use "Data\master.dta", clear

* drop year 2015, 2018 and 2019
drop if year==2018 | year==2019 | year==2015

label variable tariff_placebo "The weighted tariff difference"
label variable manu "Manufacturing"
label variable exp "Experience"
label variable expsquare "Square of Experience"
label variable sex "Female"
label variable schoolyear "Schooling years"
label variable area "Urban"

* generate interaction term between tariff difference and manufacturing
generate placebo_manu = tariff_placebo * manu
label variable placebo_manu "Tariff difference * Manufacturing"

* Run regression with dependent variable informal
reghdfe informal tariff_placebo exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) replace ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes)  

reghdfe informal tariff_placebo exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe informal tariff_placebo exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

* Run regression with dependent variable uninsured
reghdfe uninsured tariff_placebo exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe uninsured tariff_placebo exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe uninsured tariff_placebo exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table5.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

********************************************************************************
*** Table 6: Impact of Trade War on Workers in Vietnam - Restricted to Individuals Worked in Enterprises

use "Data\master.dta", clear

* drop year 2016 and 2015
drop if year<=2016

keep if ownership>2 

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & isic3<=332
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

label variable wt_tariff "The weighted tariff difference"
label variable tariff_manu "Tariff difference * Manufacturing"
label variable manu "Manufacturing"
label variable exp "Experience"
label variable expsquare "Square of Experience"
label variable sex "Female"
label variable schoolyear "Schooling years"
label variable area "Urban"
label variable informal "Informal"
label variable uninsured "Uninsured"
label variable tariff_informal "Tariff difference * Informal workers"
label variable tariff_uninsured "Tariff difference * Uninsured workers"

* Run regression with dependent variable informal
reghdfe informal wt_tariff exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)

outreg2 using "Tables\Table6.doc", label dec(3) replace ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe informal wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe informal wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 


reghdfe uninsured wt_tariff exp expsquare sex schoolyear area [pweight=weight_final], absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(All) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes)  

reghdfe uninsured wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if noag==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

reghdfe uninsured wt_tariff exp expsquare sex schoolyear area [pweight=weight_final] if trading==1, absorb(isic3 year province) cluster (isic3)
outreg2 using "Tables\Table6.doc", label dec(3) append ctitle(Trading) addtext(Industry FE, Yes, Year FE, Yes, Province FE, Yes) 

********************************************************************************
*** Table 7: Impact of Trade War on the Share of Uninsured Workers - Firm-level Analysis

use "Data\master_ves.dta", clear
drop if year<=2016

generate noag=(isic3>32)

sum wt_tariff if wt_tariff!=0
sum wt_tariff if wt_tariff!=0 & noag==1
sum wt_tariff if wt_tariff!=0 & trading==1

generate cur_uninsured = cur_employee - cur_insurance

generate share = cur_uninsured / cur_employee

label variable wt_tariff "The weighted tariff difference"

reghdfe share wt_tariff [aweight=cur_employee], absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\Table7.doc", label dec(3) replace ctitle(All) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff [aweight=cur_employee] if noag==1 , absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\Table7.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff [aweight=cur_employee] if trading==1 , absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\Table7.doc", label dec(3) append ctitle(Trading) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

keep if cur_employee>100

reghdfe share wt_tariff [aweight=cur_employee], absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\Table7.doc", label dec(3) append ctitle(All) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff [aweight=cur_employee] if noag==1 , absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\Table7.doc", label dec(3) append ctitle(Excluding agriculture and fishing) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

reghdfe share wt_tariff [aweight=cur_employee] if trading==1 , absorb(ID province isic3 year) cluster (isic3)
outreg2 using "Tables\Table7.doc", label dec(3) append ctitle(Trading) addtext(Firm FE, Yes, Industry FE, Yes, Year FE, Yes, Province FE, Yes)

log close
